package com.haidechizi.util.lock.configuration;

import com.haidechizi.util.lock.properties.CuratorProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: wangdejun
 * @create: 2019-11-12 17:28
 **/
@Slf4j
@Configuration
@EnableConfigurationProperties(CuratorProperties.class)
public class CuratorConfiguration {

    //@Value("${spring.curator.zkUrl:127.0.0.1:2181}")
    //private String zkUrl;
    @Autowired
    private CuratorProperties curatorProperties;

    @Bean
    public CuratorFramework curatorFramework() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(curatorProperties.getConnectString())
                .sessionTimeoutMs(curatorProperties.getSessionTimeoutMs())
                .connectionTimeoutMs(curatorProperties.getConnectionTimeoutMs())
                .namespace(curatorProperties.getNamespace())
                .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
        curatorFramework.start();
        log.info("curatorFramework启动成功：{}", curatorProperties.getConnectString());
        return curatorFramework;
    }
}
